package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.PurchaserAccountEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.PurchaserAccountEntityMapper;
import com.antler.smt.portal.mapper.PurchaserAccountInfoMapper;
import com.antler.smt.portal.mapper.PurchaserChargeEntityMapper;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetPurchaserAccountResp;
import com.antler.smt.service.portal.MemberAccountService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 会员账户实现接口
 *
 * @author zhangyi
 * @date 2018/3/28 17:13
 */
@Service
public class MemberAccountServiceImpl implements MemberAccountService {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Autowired
    private PurchaserAccountInfoMapper  purchaserAccountInfoMapper;

    @Autowired
    private PurchaserAccountEntityMapper purchaserAccountEntityMapper;

    /**
     * 获取会员账户信息
     *
     * @param memberId
     * @return
     */
    @Override
    public Result<GetPurchaserAccountResp> getMemberAccount(Long memberId) {
        Result result;
        result = new Result(purchaserAccountInfoMapper.getMemberAccount(memberId));
        return result;
    }

    /**
     *  采购商生成账户信息
     *
     * @param memberId
     * @return
     */
    @Override
    public Result<ResultCodeEnum> purchaserCreateAccount(Long memberId) {
        Result result = null;

        //初始化会员账户
        PurchaserAccountEntity purchaserAccountEntity = new PurchaserAccountEntity();

        //添加会员id
        purchaserAccountEntity.setMemberId(memberId);

        //添加创建时间
        purchaserAccountEntity.setCreateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //添加更新时间
        purchaserAccountEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //账户查重
        GetPurchaserAccountResp memberAccount = purchaserAccountInfoMapper.getMemberAccount(memberId);

        if(memberAccount == null){
            int i = purchaserAccountEntityMapper.insertSelective(purchaserAccountEntity);
            if(0 == i){
                result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,
                        "插入采购商信息异常！");
            }else{
                result  = new Result(ResultCodeEnum.OPERATION_SUCCESS);
            }
        }else{
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,
                    "当前采购商账户已经存在！");
        }


        return result;
    }

}
